package cn.rmc.org.service.impl;

import cn.rmc.basic.exception.BusinessException;
import cn.rmc.basic.service.impl.BaseServiceImpl;
import cn.rmc.org.domain.Employee;
import cn.rmc.org.domain.Shop;
import cn.rmc.org.domain.ShopAudit;
import cn.rmc.org.mapper.EmployeeMapper;
import cn.rmc.org.mapper.ShopAuditMapper;
import cn.rmc.org.mapper.ShopMapper;
import cn.rmc.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditMapper shopAuditMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Transactional
    @Override
    public void settlement(Shop shop) {
//        通过店铺名查询店铺
        Shop shopName = shopMapper.findByName(shop.getName());
//        如果店铺名不为空，店铺已存在
        if (shopName!=null){
            throw new BusinessException("店铺已存在");
        }
//        保存店铺
        shopMapper.save(shop);
//        查询管理员(用户名，电话，email)
        Employee employee = employeeMapper.findByAdmin(shop.getAdmin());
//        当管理员存在
        if (employee!=null){
            throw new BusinessException("用户名，电话，邮箱已被注册");
        }
//        当管理员不存在
        employee = shop.getAdmin();
//        将店铺id设置到管理员
        employee.setShop_id(shop.getId());
        employeeMapper.save(employee);

//        将管理员id设置到店铺
        shop.setAdmin(employee);
//        更新店铺数据
        shopMapper.update(shop);
    }

    @Override
    public void reject(ShopAudit shopAudit) throws Exception {
//        修改店铺状态
        Shop shop = shopMapper.loadById(shopAudit.getShop_id());
        shop.setState(-1);
        shopMapper.update(shop);
//        添加日志
        shopAudit.setAudit_time(new Date());
        Employee employee = new Employee();
        employee.setId(342L);
        shopAudit.setAuditManager(employee);
        shopAuditMapper.save(shopAudit);
//       创建复杂邮件对象
     /*   MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1435005039@qq.com");
        helper.setSubject("激活你的账户");
        helper.setText("<h1>激活你的账户</h1>"+
                "<a href='localhost:8080/employee/activation'>点击这里激活</a>",true);
        //收件人
//        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);*/
    }

    @Override
    public void pass(ShopAudit shopAudit) {
//        修改店铺状态
        Shop shop = shopMapper.loadById(shopAudit.getShop_id());
        shop.setState(1);
        shopMapper.update(shop);

//        添加日志
        shopAudit.setAudit_time(new Date());
        Employee employee = new Employee();
        employee.setId(342L);
        shopAudit.setAuditManager(employee);
        shopAuditMapper.save(shopAudit);
    }
}
